package com.zwc.core.utils

import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.StringRes
import com.zwc.core.R
import io.github.idonans.core.thread.Threads
import io.github.idonans.core.util.ContextUtil

/**
 * author:zuoweichen
 * DAte:2023-12-01 16:56
 * Description:描述
 */
object ToastUtils {
    @JvmOverloads
    fun showToast(@StringRes resId: Int) {
        Threads.runOnUi { showToast(ContextUtil.getContext().resources.getString(resId)) }
    }

    @JvmOverloads
    fun showToast(text: CharSequence?, showLongTime: Boolean = false) {
        Threads.runOnUi {
            val inflater = LayoutInflater.from(ContextUtil.getContext())
            val view = inflater.inflate(R.layout.toast_layout, null, false)
            val messageView = view.findViewById<TextView>(R.id.message)
            messageView.text = text
            showToast(view, showLongTime)
        }
    }

    @JvmOverloads
    fun showToast(view: View?, showLongTime: Boolean = false) {
        Threads.runOnUi {
            val toast = Toast(ContextUtil.getContext())
            toast.view = view
            toast.setGravity(Gravity.CENTER, 0, 0)
            toast.duration = if (showLongTime) Toast.LENGTH_LONG else Toast.LENGTH_SHORT
            toast.show()
        }
    }
}